package com.thunder.kocho.circle;


import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 生成来回移动的白色渐变光带,作用于圆形区域
 * @author Hilex
 * @date 2021-3-4
 */
public class CircleBelt extends View {

    private final static int DEFAULT_HEIGHT=100;
    private final static int DEFAULT_WIDTH=100;

    private float mBeltWidthPercentage=0.24f;
    private int mBeltCycle=2000;

    private int mWidth;
    private int mHeight;
    private Rect mRect;
    private Paint mPaint;

    private ObjectAnimator mObjectAnimator;

    private float[] positions;
    private final int[] colors={Color.parseColor("#00000000"),Color.parseColor("#00000000"),Color.parseColor("#FFFFFFFF"),Color.parseColor("#00000000"),Color.parseColor("#00000000")};

    private float mRadius;

    public CircleBelt(Context context){
        this(context,null);
    }

    public CircleBelt(Context context, AttributeSet attrs){
        this(context,attrs,0);
    }

    public CircleBelt(Context context, AttributeSet attrs, int defStyle){
        super(context,attrs,defStyle);
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth=w;
        mHeight=h;
        mRect=new Rect(0,0,w,h);
        mRadius=Math.min(w,h)/2f;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height=DEFAULT_HEIGHT;
        int width=DEFAULT_WIDTH;

        int HEIGHT_MODE=MeasureSpec.getMode(heightMeasureSpec);
        int HEIGHT_SIZE=MeasureSpec.getSize(heightMeasureSpec);
        int WIDTH_MODE=MeasureSpec.getMode(widthMeasureSpec);
        int WIDTH_SIZE=MeasureSpec.getSize(widthMeasureSpec);

        if(HEIGHT_MODE==MeasureSpec.EXACTLY){
            height=HEIGHT_SIZE;
        }else if(HEIGHT_MODE==MeasureSpec.AT_MOST){
            height=Math.min(HEIGHT_SIZE,height);
        }
        if(WIDTH_MODE==MeasureSpec.EXACTLY){
            width=WIDTH_SIZE;
        }else if(WIDTH_MODE==MeasureSpec.AT_MOST){
            width=Math.min(width,WIDTH_SIZE);
        }
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mRect!=null){
            canvas.drawCircle(mRect.centerX(),mRect.centerY(),mRadius,mPaint);
        }
    }

    private void init(){
        positions=new float[5];
        positions[0]=0;
        positions[4]=1;
        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setDither(true);
        PropertyValuesHolder holder=PropertyValuesHolder.ofFloat("OffsetPercentage",-mBeltWidthPercentage,1);
        mObjectAnimator=ObjectAnimator.ofPropertyValuesHolder(this,holder);
        mObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mObjectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mObjectAnimator.setDuration(mBeltCycle);
    }

    private void setOffsetPercentage(float percentage){
        positions[1]=percentage;
        positions[2]=percentage+mBeltWidthPercentage/2f;
        positions[3]=percentage+mBeltWidthPercentage;
        LinearGradient linearGradient=new LinearGradient(0,0,mWidth,mHeight,colors,positions, Shader.TileMode.CLAMP);
        mPaint.setShader(linearGradient);
        invalidate();
    }

    /**
     * 延迟开启动画
     * @param delay 延迟的毫秒数
     */
    public void postDelayAnimation(int delay) {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if (mObjectAnimator != null) {
                    if (!mObjectAnimator.isStarted()) {
                        post(() -> {
                            setVisibility(VISIBLE);
                            mObjectAnimator.start();
                        });
                    }
                }
            }
        };
        timer.schedule(task, delay);
    }

    public void endAnimation(){
        if(mObjectAnimator.isStarted()){
            mObjectAnimator.end();
        }
    }


    public void setBeltWidthPercentage(float beltWidthPercentage) {
        this.mBeltWidthPercentage = beltWidthPercentage;
    }

    public void setBeltCycle(int beltCycle) {
        this.mBeltCycle = beltCycle;
    }
}